package com.code.ddbond.controller;

import com.code.ddbond.vo.RegisterVo;
import com.code.ddbond.pojo.UserInfo;
import com.code.ddbond.service.UserInfoService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;

@Controller
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/register")
    public String register(RegisterVo registerVo, Model model) {
        if (userInfoService.register(registerVo)) {
            return "login";
        } else {
            model.addAttribute("error", "注册账户已存在请重试");
            return "register";
        }
    }

    @GetMapping("/checkRegistername")
    @ResponseBody
    public ResponseEntity<?> checkLoginname(@RequestParam String loginname)
    {
        boolean exists = userInfoService.checkLoginnameExists(loginname);
        return ResponseEntity.ok(Collections.singletonMap("exists", exists));
    }

    @GetMapping("/mainpage")
    public String showmainpage(Model model) {
        return "mainpage";
    }

    @PostMapping("/login")
    public String processLogin(@RequestParam("loginname") String loginName,
                               @RequestParam("password") String password,
                               Model model, HttpSession session)
    {

        if (userInfoService.login(loginName, password) > 0) {
            UserInfo user = userInfoService.getUserByLoginName(loginName);
            session.setAttribute("userId", user.getId());
            return "mainpage";
        } else {
            model.addAttribute("error", "无效的登录名或者密码");
            return "login";
        }
    }


    @GetMapping("/updateuserinfo")
    public String showupdateUserInfopage(Model model) {
        return "updateuserinfo";
    }

    @PostMapping("/updateuserinfo")
    public String updateUser( UserInfo userInfo, Model model)
    {
        try {
            userInfoService.updateUserById(userInfo);
            return "mainpage";
        } catch (IllegalArgumentException e) {
            model.addAttribute("message", e.getMessage());
            return "updateuserinfo";
        }
    }

    @GetMapping("/findbyname")
    public String showfindbynamepage()
    {
        return "findbyname";
    }

    @PostMapping("/findbyname")
    public String findById(@RequestParam("username")String username, Model model)
    {
        UserInfo u = userInfoService.getUserInfoByName(username);
        if(u != null)
        {
            model.addAttribute("userInfo", u);
            return "findbyname_success";
        }
        else
        {
            model.addAttribute("error","未找到该用户名请重试");
            return "findbyname";
        }
    }

    @GetMapping("/resetpassword")
    public String showsubmitemailpage()
    {
        return "resetpassword";
    }

    @PostMapping("/resetpassword")
    public String processResetPasswordForm(@RequestParam("email") String email, @RequestParam("newPassword") String newPassword, Model model) {
        if (userInfoService.checkEmailForPasswordRecovery(email)) {
            try {
                userInfoService.resetPassword(email, newPassword);
                // 密码更新成功，返回主界面
                return "mainpage";
            } catch (IllegalArgumentException e) {
                // 更新密码失败
                model.addAttribute("error", "修改密码失败.");
                return "resetpassword";
            }
        } else {
            // 邮箱不存在
            model.addAttribute("error", "Email not found.");
            return "resetpassword";
        }
    }


}
